
from pyglet import gl
from pyglet.sprite import Sprite
from math import cos
from math import sin
from math import atan
from math import degrees

from common import *

class Arrow():
	
	def __init__(self, angle, power, dammage, x, y):
		self.speed = cos(angle) * power
		self.vertical = sin(angle) * power
		self.gravity = 300
		self.dammage = dammage
		self.x = x
		self.y = y
		self.name = "arrow"
		self.delete = False
		self.sprite = Sprite(load_image("arrow.png"), x, y)
	
	def update(self, dt):
		self.x += self.speed * dt
		self.vertical -= self.gravity * dt
		self.y += self.vertical * dt
		self.sprite.rotation = degrees(atan(self.speed / self.vertical)) + 90
		if (self.vertical > 0):
			self.sprite.rotation += 180
		self.sprite.x = self.x
		self.sprite.y = self.y
		if (map.is_ground(self.x, self.y)):
			self.delete = True
		else:
			for other in Game.sprites:
				if (not other.name in ("player", "arrow", "spawn") and other.contains(self.x, self.y)):
					if not other.name in ("cactus", "goal"):
						load_music("hit.ogg", streaming=False).play()
						other.health -= self.dammage
						other.jump()
						other.killed_by = "player"
					self.delete = True
	
	def draw(self, offset):
		self.sprite.x -= offset
		self.sprite.draw()
